package com.daocao.hua.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.V3MyFundDetailAdapter;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.MyFundDetailV3Bean;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.OnLoadeMoreListener;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-30.
 */

public class V3MyFundDetailFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private V3MyFundDetailAdapter adapter;
    private int type;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getFundDetailV3(page);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_fund_detail;
    }

    @Override
    protected void initView() {
        int position = getArguments().getInt("type");
        switch (position) {
            case 0:
                type = 0;
                break;
            case 1:
                type = 1;
                break;
            case 2:
                type = 4;
                break;
            case 3:
                type = 8;
                break;
            case 4:
                type = 23;
                break;
        }
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getFundDetailV3(1);
            }
        });
        adapter = new V3MyFundDetailAdapter(getContext());
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);
        onLoadeMoreListener.setPage();
        getFundDetailV3(1);
    }


    public void getFundDetailV3(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        MyFundDetailV3Bean bean = new MyFundDetailV3Bean();
        bean.page = page;
        if (type != 0) {
            bean.type = type;
        }
        Api.api_service.getFundDetailV3(bean).compose(RxLifeUtil.checkOn(getContext())).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    adapter.add(result.data, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
